//-----------------------------------------------------------------------------
// Advanced Water - all about Water Vertex Shader
//-----------------------------------------------------------------------------

#include "AdvancedWaterCommon.hlsl"

#if ADVANCEDWATER_ID == ADVANCEDWATER_TYPE_REFRACTIONMASK


//////////////////////////////////////////////
	// vs.1.1

	VSOUT ProcessVertex( VSIN _input )
	{
		VSOUT Output = (VSOUT)0;

		Output.Position = mul(_input.Position, g_mWorldViewProj);

		return Output;
	}

#elif ADVANCEDWATER_ID == ADVANCEDWATER_TYPE_SURFACERENDER

	uniform float3	g_vCameraPos;
	uniform float3	g_vLightPos;
	uniform float4	g_vWaveParams;
	uniform float2	g_vWaveDir;

	VSOUT ProcessVertex( VSIN _input )
	{
		VSOUT Output = (VSOUT)0;
		float3x3 objToTangentSpace;
		objToTangentSpace[0] = float3(1, 0, 0);
		objToTangentSpace[1] = float3(0, 1, 0);
		objToTangentSpace[2] = float3(0, 0, 1);

		// g_vWaveParams.y is water's height
		// g_vWaveParams.z is scale
		// Wave0.z is water's height - terrain's height
		Output.Wave0.z = (g_vWaveParams.y - _input.Position.z) / g_vWaveParams.z;
		
		_input.Position.z = g_vWaveParams.y;
		Output.Position = mul(float4(_input.Position.xyz, 1), g_mWorldViewProj);

		float2 fTranslation = float2( fmod(g_vWaveParams.x, 100) * g_vWaveDir.xy );
		float2 vTexCoords = _input.Position.xy * g_vWaveParams.w;

		// Output bump layers texture coordinates
		float fSinTranslation = sin(fTranslation*100) * 0.005f;
		float2 vTranslation0 = fTranslation + fSinTranslation;
		float2 vTranslation1 = fTranslation - fSinTranslation;
		float2 vTranslation2 = fTranslation;

		// Scale texture coordinates to get mix of low/high frequency details
		Output.Wave0.xy = vTexCoords.xy + fTranslation*2;
		Output.Wave1.xy = vTexCoords.xy*2 + fTranslation*4;
		Output.Wave2.xy = vTexCoords.xy*4 + fTranslation*2;
		Output.Wave3.xy = vTexCoords.xy*8 + fTranslation;

		float4 vHpos = Output.Position;
		Output.Wave0.w = 0;
		vHpos.y = -vHpos.y;

		Output.ScreenPos.xy = (vHpos.xy + vHpos.w) * 0.5f;
		Output.ScreenPos.zw = float2(1, vHpos.w);

		float3 EyeVec = g_vCameraPos.xyz - _input.Position.xyz;
		Output.Eye.xyz = normalize(mul(EyeVec, objToTangentSpace));
		
		Output.Wave0.z = Output.Wave0.z / abs(normalize(EyeVec).z);
		//Output.Wave0.z = Output.Wave0.z * Output.Wave0.z * length(EyeVec) / dot(EyeVec, float3(0,0,Output.Wave0.z));

		float3 LightVec = g_vLightPos.xyz;
		Output.Light.xyz = normalize(mul(LightVec, objToTangentSpace));

		return Output;
	}

#else
// ------------------------------------------------------------------------------------------------
// DEFAULT
// ------------------------------------------------------------------------------------------------
    VSOUT ProcessVertex( VSIN _input )
    {
        VSOUT Output = (VSOUT)0;

        return Output;
    }

#endif